package org.dropme.server.server;

import java.io.IOException;
import java.util.Date;

import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dropme.server.PMF;
import org.dropme.server.model.Litter;

public class DropLitterServlet extends HttpServlet {

	private static final long serialVersionUID = -5594966551540503836L;

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String title = (String) req.getParameter("title");
		String description = (String) req.getParameter("description");
		Date date = new Date();
		String user = (String) req.getParameter("user");
		String lat, lon;
		if ((lat = req.getParameter("latitude")) != null
				&& (lon = req.getParameter("longitude")) != null) {
			Double latitude = Double.parseDouble(lat);
			Double longitude = Double.parseDouble(lon);
			Litter litter = new Litter(title, description, date, user, latitude, longitude);
			litter.setGeoTag(latitude, longitude);
			PersistenceManager manager = PMF.get().getPersistenceManager();
			litter = manager.makePersistent(litter);
			resp.getWriter().append(litter.getKey().toString());
			resp.getWriter().append(" <br>" + litter.getGeoTag());
		} else {
			resp.getWriter().append("-1");
		}
		
		
	}
	
}
